#include "Client_IO.h"

Client_IO::Client_IO()
{
	endGame		= false;
	value		= 0;
	gameState	= ACRES;
}

void Client_IO::Run()
{
	printf("Please enter server IP address: ");				//Print the IP address message
	cin >> ip;												//Get the user's ip address they want to connect to (which they send as input)
	command.encode(SSpawn, 0, ip);							//Encode the user's ip address they want to connect to
	comm.Send(&command);									//Send initial command containing the ip to gameState

	while(!endGame)
	{
		switch(gameState)
		{
		case ACRES:
			{
				do											//While the returned command is an error
				{
					printf( command.getString() );			//Get the first command for each state (which will be that states message)

					cin >> value;							//Get the amount of acres the user wants to buy or sell
					command.encode(SBarter, value, "");		//Encode the command object as the value the user just input
					comm.Send(&command);					//Send the command that was just encoded and "sent down the line"
				}
				while(command.getCmdType() == Error);

				gameState = FEED;							//Set the game state accordingly

				break;
			}
		case FEED:
			{
				do											//While the returned command is an error
				{
					printf( command.getString() );			//Get the first command for each state (which will be that states message)

					cin >> value;							//Get the amount of bushels the user wants to feed to their people
					command.encode(SFeed, value, "");		//Encode the command object as the value the user just input
					comm.Send(&command);					//Send the command that was just encoded and "sent down the line"
				}
				while(command.getCmdType() == Error);

				gameState = PLANT;							//Set the game state accordingly

				break;
			}
		case PLANT:
			{
				do											//While the returned command is an error
				{
					printf( command.getString() );			//Get the first command for each state (which will be that states message)

					cin >> value;							//Get the amount of bushels the user wants to plant
					command.encode(SPlant, value, "");		//Encode the command object as the value the user just input
					comm.Send(&command);					//Send the command that was just encoded and "sent down the line"
				}
				while(command.getCmdType() == Error);

				gameState = ACRES;							//Set the game state accordingly

				break;
			}
		}

		if(command.getCmdType() == RQuit)					//If the returned command's type is quit
		{
			endGame = true;									//Set the endGame variable appropriately
		}
	}
	//final report
	command.encode(SQuit, 0, 0);
	comm.Send(&command);
	printf(command.getString());
}